package com.ruoyi.project.simple.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.project.simple.domain.vo.SplOrderVO;
import com.ruoyi.project.system.domain.SysUser;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.simple.domain.SplOrder;
import com.ruoyi.project.simple.service.ISplOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 送检样品订单Controller
 *
 * @author zhujj
 * @date 2020-01-09
 */
@RestController
@RequestMapping("/simple/order")
public class SplOrderController extends BaseController {
    @Autowired
    private ISplOrderService splOrderService;

    @Autowired
    private WxPayService wxService;

    private static final Logger log = LoggerFactory.getLogger(SplOrderController.class);
    /**
     * 查询送检样品订单列表
     */
    @PreAuthorize("@ss.hasPermi('simple:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SplOrder splOrder) {
        startPage();
        List<SplOrder> list = splOrderService.selectSplOrderList(splOrder);
        return getDataTable(list);
    }

    /**
     * 导出送检样品订单列表
     */
    @PreAuthorize("@ss.hasPermi('simple:order:export')")
    @Log(title = "送检样品订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SplOrder splOrder) {
        List<SplOrder> list = splOrderService.selectSplOrderList(splOrder);
        ExcelUtil<SplOrder> util = new ExcelUtil<SplOrder>(SplOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取送检样品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('simple:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(splOrderService.selectSplOrderById(orderId));
    }

    /**
     * 新增送检样品订单
     */
    @PreAuthorize("@ss.hasPermi('simple:order:add')")
    @Log(title = "送检样品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SplOrderVO splOrder) throws WxPayException {
        SplOrderVO splOrderVO = splOrderService.insertSplOrder(splOrder);
        return this.wxPay(splOrderVO);
    }

    /**
     * 新增送检样品订单
     */
    @Log(title = "生成预支付订单", businessType = BusinessType.INSERT)
    @PostMapping(value = "/wxPay")
    public AjaxResult wxPay(@RequestBody SplOrderVO splOrderVO) throws WxPayException {
        //生成预支付订单
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setBody("样品检验订单：" + splOrderVO.getOrderNumber());
        request.setProductId(splOrderVO.getOrderId());

        log.debug("-------------------------------生成预支付订单："+splOrderVO.getOrderId()+"----------------------------");
        SysUser user = SecurityUtils.getLoginUser().getUser();
        request.setOpenid(user.getOpenId());
        request.setOutTradeNo(splOrderVO.getOrderId());
        request.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        request.setSignType("MD5");
        request.setTradeType("JSAPI");
        Long actualAmount = splOrderVO.getActualAmount().multiply(new BigDecimal(100)).longValue();
        request.setTotalFee(actualAmount.intValue());
        WxPayUnifiedOrderResult wxPayUnifiedOrderResult = wxService.unifiedOrder(request);
        //生成支付订单成功
        if (wxPayUnifiedOrderResult.getResultCode().equals("SUCCESS")) {

            return AjaxResult.success("支付成功！",wxPayUnifiedOrderResult);
        } else {
            return AjaxResult.error(wxPayUnifiedOrderResult.getErrCodeDes());
        }
    }

    /**
     * 修改送检样品订单
     */
    @PreAuthorize("@ss.hasPermi('simple:order:edit')")
    @Log(title = "送检样品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SplOrder splOrder) {
        return toAjax(splOrderService.updateSplOrder(splOrder));
    }

    /**
     * 修改送检样品订单
     */
    @PreAuthorize("@ss.hasPermi('simple:order:edit')")
    @Log(title = "送检样品订单", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/acceptOrder")
    public AjaxResult acceptOrder(@RequestBody SplOrder splOrder) {
        return toAjax(splOrderService.acceptOrder(splOrder));
    }

    /**
     * 删除送检样品订单
     */
    @PreAuthorize("@ss.hasPermi('simple:order:remove')")
    @Log(title = "送检样品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds) {
        return toAjax(splOrderService.deleteSplOrderByIds(orderIds));
    }
}
